Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Google Medium
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Minimizing Array After Replacing Pairs With Their Product

Problem Statement

You are given an array containing integers nums and an integer value k. You can perform the below operation on the array elements for multiple times:

  • Pick two adjacent elements, x and y, from nums and replace them with a single element equal to their product, x*y, but only if x*y is less than or equal to k.

Return the minimum possible length of nums after performing multiple operations.

Examples

  • Example 1:

    • Input: nums = [2, 3, 4, 5], k = 10
    • Expected Output: 3
    • Justification: We can replace 2 and 3 with their product 6 ([6, 4, 5]). No further operations can be performed since all adjacent products will exceed k. Hence, the minimum length is 3.
  • Example 2:

    • Input: nums = [1, 2, 2, 3], k = 5
    • Expected Output: 2
    • Justification: First, replace 2 and 2 with 4 ([1, 4, 3]), then replace 1 and 4 with 4 ([4, 3]). No more operations can be performed, resulting in a minimum length of 2.
  • Example 3:

    • Input: nums = [10, 5, 2, 3, 4, 2, 20, 1], k = 50
    • Expected Output: 3
    • Justification: First, replace 10 and 5 with 50 ([50, 2, 3, 4, 2, 20, 1]), then replace 2, 3, 4 and 2 with 48 ([50, 48, 20, 1]), and then replace 20 and 1 with 20 ([50, 48, 20]). No more operations can be performed, resulting in a minimum length of 3.

Solution

To solve this problem, we will iterate through the array and look for pairs of adjacent elements that can be replaced with their product without exceeding k. This process will be repeated until no more operations can be performed. The rationale behind this approach is to gradually reduce the array's length by merging elements that comply with the given condition, ensuring the final array is as short as possible. This method is efficient because it directly targets pairs that meet the condition and reduces the array size step by step, preventing unnecessary comparisons and operations.

We believe this approach will work because it iteratively consolidates adjacent elements under a clear condition, ensuring the array's length is minimized without overlooking any potential operations. By focusing on adjacent pairs, we take advantage of the problem's constraint on element selection, making the approach straightforward and effective in reaching the solution.

Step-by-Step Algorithm

  1. Initialization: Start by creating a dynamic list, res, to hold the potentially minimized array elements.

  2. Iterate Through Input Array: Loop through each element, num, in the given nums array.

    • Zero Check: If num equals 0, immediately return 1 as the minimum possible length because multiplying by zero will always meet the condition, and we can reduce the array to a single zero element.
  3. Check Last Element in res:

    • If res is not empty, check the product of the last element in res and num. If this product is less than or equal to k, it means we can combine these two numbers.
      • Combine: Update the last element in res with the product of the last element and num.
    • Else, if the product is greater than k or res is empty, append num to res.
  4. Continue Until End: Repeat steps 2 and 3 until you have processed all elements in the input nums array.

  5. Return Result: The length of res is the minimized length of the array. Return this length.

Algorithm Walkthrough

Let's consider the Input: nums = [10, 5, 2, 3, 4, 2, 20, 1], k = 50

  1. Initialization: Create an empty list res.

  2. First Element (10): res is empty, so add 10 to res. res becomes [10].

  3. Second Element (5): The product of 10 (last element in res) and 5 is 50, which is equal to k. Update the last element in res with this product. res becomes [50].

  4. Third Element (2): The product of 50 and 2 is 100, which is greater than k. Add 2 to res. res becomes [50, 2].

  5. Fourth Element (3): The product of 2 and 3 is 6, which is less than k. Combine these into 6. res becomes [50, 6].

  6. Fifth Element (4): The product of 6 and 4 is 24, which is less than k. Combine these into 24. res becomes [50, 24].

  7. Sixth Element (2): The product of 24 and 2 is 48, which is less than k. Combine these into 48. res becomes [50, 48].

  8. Seventh Element (20): The product of 48 and 20 is 960, which is greater than k. Add 20 to res. res becomes [50, 48, 20].

  9. Eighth Element (1): The product of 20 and 1 is 20, which is less than k. Combine these into 20. res becomes [50, 48, 20].

  10. End of Input Array: We've processed all elements in nums.

  11. Return Result: The length of res is 3. This is the minimum possible length of the array after performing the operations.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

  • The given algorithm iterates through the input array exactly once. For each element in the array, the operations performed (checking conditions, updating the list, and adding elements to the list) are constant time operations. Therefore, the time complexity is linear in the size of the input array, n, where n is the number of elements in the array.

Space Complexity

  • In the worst-case scenario, none of the adjacent elements can be combined (i.e., the product of any two adjacent elements always exceeds k). In this case, the algorithm will store all elements of the input array in the res list. Therefore, the space complexity is also linear in the size of the input array.
Minimizing Array After Replacing Pairs With Their Product (medium)
Fair Distribution of Cookies (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity